package com.le.sarrs.web.conf;

import com.le.sarrs.cms.CmsHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import reactor.core.Environment;
import reactor.core.Reactor;
import reactor.core.spec.Reactors;
import reactor.spring.context.config.EnableReactor;
import reactor.spring.webmvc.PromiseHandlerMethodReturnValueHandler;

import java.util.List;

/**
 * reactor配置
 *
 * @author yangchao 2017年1月26日
 */
@Configuration
@EnableReactor
public class ReactorConfig extends WebMvcConfigurationSupport {

    final static Logger logger = LoggerFactory.getLogger("init");

    /**
     * 业务逻辑reactor
     *
     * @param env
     * @return
     */
    @Bean(name = "reactor")
    public Reactor reactor(Environment env) {
        return Reactors.reactor().env(env).dispatcher(Environment.RING_BUFFER).get();
        // return
        // Reactors.reactor().env(env).dispatcher(Environment.THREAD_POOL).get();
    }

    @Bean(name = "preactor")
    public Reactor preactor(Environment env) {
        return Reactors.reactor().env(env).dispatcher(Environment.THREAD_POOL).get();
    }

    @Bean(name = "sarrsInit")
    public boolean sarrsInit() {
        logger.info("init start.");
        CmsHandler.init();
//        CacheUtil.getCache("111");
//        SpringUtil.get("userClientService");
//        SpringUtil.get("userService");
//        SpringUtil.get("customerService");
//        SpringUtil.get("customerClientService");
//        SpringUtil.get("androidSearchService");
//        SpringUtil.get("androidUserService");
//        SpringUtil.get("androidCustomerService");
//        SpringUtil.get("androidMessageService");
//        SpringUtil.get("recallSchemeService");
//        SpringUtil.get("nearbyService");
//        SpringUtil.get("messageClientService");
//        SpringUtil.get("messageService");
//        SpringUtil.get("dictionaryClientService");
//        SpringUtil.get("androidConfigService");
//        SpringUtil.get("userNearbyService");
//        SpringUtil.get("searchService");
//        SpringUtil.get("androidPayService");
//        SpringUtil.get("citySquareService");
        return true;
    }

    @Override
    protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(0, new PromiseHandlerMethodReturnValueHandler());
    }

}
